﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DockSample
{
    public partial class NFRChartDialog : Form
    {
        List<NonFunctionRequirment> tmpNFRList = new List<NonFunctionRequirment>();
        public List<NonFunctionRequirment> TmpNFRList
        {
            get { return tmpNFRList; }
            //set { tmpNFRList = value; }
        }
        public string selectedNFRName()
        {
            return comboBoxNFRName.Text;
        }
        //List<Point> tmpPointList = new List<Point>();
        OriginalNFRChartType chartType = null;
        public OriginalNFRChartType ChartType
        {
            get { return chartType; }
            set { chartType = value; }
        }
        Axis axis = new Axis();

        Pen pen = new Pen(Color.Blue, 1);
        Font font = new Font("Arial", 8);
        SolidBrush brush = new SolidBrush(Color.Black);

        private string unit = "";
        public string Unit
        {
            get { return unit; }
        }
        public NFRChartDialog(List<NonFunctionRequirment> NFRList)
        {
            InitializeComponent();

            resetPointLabel();
            this.tmpNFRList = NFRList;
            initChartTypyComBox();
            initUnitComBox();

            initNFRNameComBox();

        }

        private void initChartTypyComBox()
        {
            this.comboBoxChartType.Items.Add("TypeOne");
            this.comboBoxChartType.Items.Add("TypeTwo");
            this.comboBoxChartType.Items.Add("TypeThree");
            this.comboBoxChartType.Items.Add("TypeFour");
            this.comboBoxChartType.Items.Add("TypeFive");
            this.comboBoxChartType.Items.Add("TypeSix");
            this.comboBoxChartType.Items.Add("TypeSeven");

            this.comboBoxChartType.SelectedIndex = 0;
        }

        private void initUnitComBox()
        {
            this.comboBoxUnit.Items.Add("Year");
            this.comboBoxUnit.Items.Add("Month");
            this.comboBoxUnit.Items.Add("Week");
            this.comboBoxUnit.Items.Add("Day");
            this.comboBoxUnit.Items.Add("Second");
            this.comboBoxUnit.Items.Add("");
            this.comboBoxUnit.Items.Add("");
            this.comboBoxUnit.Items.Add("");

            if (TmpNFRList[0].ChartType != null)
            {
                this.comboBoxUnit.SelectedItem = tmpNFRList[0].Unit;
            }
            else
            {
                this.comboBoxUnit.SelectedIndex = 0;
            }
        }

        private void initNFRNameComBox()
        {
            if (TmpNFRList != null && TmpNFRList.Count != 0)
            {
                foreach (NonFunctionRequirment nfr in TmpNFRList)
                {
                    this.comboBoxNFRName.Items.Add(nfr.nonFunctionName);
                }
                this.comboBoxNFRName.SelectedIndex = 0;

                if(TmpNFRList[0].ChartType != null)
                {
                    this.comboBoxChartType.SelectedItem = tmpNFRList[0].ChartType.getChartType();

                    this.comboBoxUnit.SelectedItem = tmpNFRList[0].Unit;
                    this.ChartType = TmpNFRList[0].ChartType;
                    controlPointLabelByChartType(TmpNFRList[0].ChartType);
                }
            }
        }
        
        private void resetPointLabel()
        {
            this.firstPointLabel.Visible = false;
            this.secondPointLabel.Visible = false;
            this.thirdPointLabel.Visible = false;

            this.firstPointLabel.Text = "(_,_)";
            this.secondPointLabel.Text = "(_,_)";
            this.thirdPointLabel.Text = "(_,_)";
        }
        //
        private void comboBoxNFRName_SelectedValueChanged(object sender, EventArgs e)
        {
            foreach (NonFunctionRequirment NFR in TmpNFRList)
            {
                if (comboBoxNFRName.Text.Equals(NFR.NonFunctionName) && NFR.ChartType != null)
                {
                    this.comboBoxChartType.SelectedItem = NFR.ChartType.getChartType();
                    this.comboBoxUnit.SelectedItem = NFR.Unit;
                    this.chartType = NFR.ChartType;
                    //坐标标签的现实控制
                    controlPointLabelByChartType(NFR.ChartType);
                    Invalidate();
                    break;
                }
            }
        }

        private void controlPointLabelByChartType(OriginalNFRChartType chartType)
        {
            if (chartType is TypeOne)
            {
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(" + chartType.X1 + ",1)";
                this.firstPointLabel.Location = chartType.getFirstLabelLocation();
            }
            else if (chartType is TypeTwo)
            {
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(" + chartType.X1 + ",1)";
                this.firstPointLabel.Location = chartType.getFirstLabelLocation();

                this.secondPointLabel.Visible = true;
                this.secondPointLabel.Text = "(" + chartType.X2 + ",0)";
                this.secondPointLabel.Location = chartType.getSecondLabelLocation();
            }
            else if (chartType is TypeThree)
            {
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(" + chartType.X1 + ",1)";
                this.firstPointLabel.Location = chartType.getFirstLabelLocation();

                this.secondPointLabel.Visible = true;
                this.secondPointLabel.Text = "(" + chartType.X2 + ",0)";
                this.secondPointLabel.Location = chartType.getSecondLabelLocation();
            }
            else if (chartType is TypeFour)
            {
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(" + chartType.X1 + ",0)";
                this.firstPointLabel.Location = chartType.getFirstLabelLocation();

                this.secondPointLabel.Visible = true;
                this.secondPointLabel.Text = "(" + chartType.X2 + ",1)";
                this.secondPointLabel.Location = chartType.getSecondLabelLocation();
            }
            else if (chartType is TypeFive)
            {
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(" + chartType.X1 + ",1)";
                this.firstPointLabel.Location = chartType.getFirstLabelLocation();
            }
            else if (chartType is TypeSix)
            {
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(" + chartType.X1 + ",1)";
                this.firstPointLabel.Location = chartType.getFirstLabelLocation();
            }
            else if (chartType is TypeSeven)
            {
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(" + chartType.X1 + ",1)";
                this.firstPointLabel.Location = chartType.getFirstLabelLocation();

                this.secondPointLabel.Visible = true;
                this.secondPointLabel.Text = "(" + chartType.X2 + ",1)";
                this.secondPointLabel.Location = chartType.getSecondLabelLocation();

                this.thirdPointLabel.Visible = true;
                this.thirdPointLabel.Text = "(" + chartType.X3 + ",0)";
                this.thirdPointLabel.Location = chartType.getThirdLabelLocation();

            }
        }
        private void comboBoxChartType_SelectedValueChanged(object sender, EventArgs e)
        {
            Graphics g = CreateGraphics();
            int labelHeight = 15;
            resetPointLabel();

            if(this.comboBoxChartType.Text.Equals("TypeOne"))
            {
                ChartType = new TypeOne();
                
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(_,1)";
                this.firstPointLabel.Location = new Point(100 - labelHeight, 200 - labelHeight);
            }
            else if (this.comboBoxChartType.Text.Equals("TypeTwo"))
            {

                ChartType = new TypeTwo();
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(_,1)";
                //OriginalNFRChartType类的坐标微调魔数
                this.firstPointLabel.Location = new Point(150 - labelHeight, 200 - labelHeight);

                this.secondPointLabel.Visible = true;
                this.secondPointLabel.Text = "(_,0)";
                this.secondPointLabel.Location = new Point(250 - labelHeight, 250 + 2);
            }
            else if (this.comboBoxChartType.Text.Equals("TypeThree"))
            {

                ChartType = new TypeThree();
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(_,1)";
                //OriginalNFRChartType类的坐标微调魔数
                this.firstPointLabel.Location = new Point(150 - labelHeight, 200 - labelHeight);

                this.secondPointLabel.Visible = true;
                this.secondPointLabel.Text = "(_,0)";
                this.secondPointLabel.Location = new Point(250 - labelHeight, 250 + 2);
            }
            else if (this.comboBoxChartType.Text.Equals("TypeFour"))
            {

                ChartType = new TypeFour();
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(_,0)";
                //OriginalNFRChartType类的坐标微调魔数
                this.firstPointLabel.Location = new Point(100 - labelHeight, 250 + 2);

                this.secondPointLabel.Visible = true;
                this.secondPointLabel.Text = "(_,1)";
                this.secondPointLabel.Location = new Point(150 - labelHeight, 200 - labelHeight);
            }
            else if (this.comboBoxChartType.Text.Equals("TypeFive"))
            {

                ChartType = new TypeFive();
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(_,1)";
                this.firstPointLabel.Location = new Point(100 - labelHeight, 200 - labelHeight);

                
            }
            else if (this.comboBoxChartType.Text.Equals("TypeSix"))
            {

                ChartType = new TypeSix();
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(_,1)";
                this.firstPointLabel.Location = new Point(150 - labelHeight, 200 - labelHeight);

            }
            else if (this.comboBoxChartType.Text.Equals("TypeSeven"))
            {

                ChartType = new TypeSeven();
                this.firstPointLabel.Visible = true;
                this.firstPointLabel.Text = "(_,1)";
                this.firstPointLabel.Location = new Point(100 - labelHeight, 200 - labelHeight);

                this.secondPointLabel.Visible = true;
                this.secondPointLabel.Text = "(_,1)";
                this.secondPointLabel.Location = new Point(200 - labelHeight, 200 - labelHeight);

                this.thirdPointLabel.Visible = true;
                this.thirdPointLabel.Text = "(_,0)";
                this.thirdPointLabel.Location = new Point(250 - labelHeight, 250 + 2);
            }
            Invalidate();
        }
        //绘图
        private void NFRChartDialog_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = CreateGraphics();

            axis.draw(g);
            if (chartType != null)
            {
                chartType.draw(g);
            }

            g.DrawString("X / " + Unit, font, brush, new Point(300, 250));
        }
        //
        private void comboBoxUnit_SelectedValueChanged(object sender, EventArgs e)
        {
            this.unit = comboBoxUnit.Text;
            Invalidate();
        }
        #region //PointLabel事件函数

        private void firstPointLabel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                AxisLabelAddDialog addDialog = new AxisLabelAddDialog();
                string text = firstPointLabel.Text;
                text = text.Substring(1, text.Length - 2);
                string xValue = text.Substring(0, text.IndexOf(","));
                string yValue = text.Substring(text.IndexOf(",") + 1);

                if (!xValue.Equals("_"))
                {
                    addDialog.XValue = xValue;
                }

                if (!yValue.Equals("_"))
                {
                    addDialog.YValue = yValue;
                }

                addDialog.setYTextBoxUnenable();

                if (addDialog.ShowDialog() == DialogResult.OK)
                {
                    string txt = secondPointLabel.Text;
                    txt = txt.Substring(1, txt.Length - 2);
                    string xSecondLabelValue = txt.Substring(0, txt.IndexOf(","));

                    //secondLablValue的X轴有数值，则需要与firstPointLabel的X轴比较，保证前者大于后者
                    if (StringUtil.isNumberic(xSecondLabelValue))
                    {
                        double secondLabelXNum = Convert.ToDouble(xSecondLabelValue);

                        if (StringUtil.isNumberic(addDialog.XValue) && Convert.ToDouble(addDialog.XValue) > 0 && secondLabelXNum > Convert.ToDouble(addDialog.XValue))
                        {
                            //TypeSeven单独处理
                            if (ChartType is TypeSeven)
                            {
                                string txt3 = thirdPointLabel.Text;
                                txt3 = txt3.Substring(1, txt3.Length - 2);
                                string x3SecondLabelValue = txt3.Substring(0, txt3.IndexOf(","));

                                if (StringUtil.isNumberic(x3SecondLabelValue))
                                {
                                    if (Convert.ToInt32(x3SecondLabelValue) > Convert.ToDouble(addDialog.XValue))
                                    {
                                        firstPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                        ChartType.X1 = Convert.ToInt32(addDialog.XValue);

                                        ChartType.adjustChart();
                                        Invalidate();

                                        this.firstPointLabel.Location = ChartType.getFirstLabelLocation();
                                        this.secondPointLabel.Location = ChartType.getSecondLabelLocation();
                                        this.thirdPointLabel.Location = ChartType.getThirdLabelLocation();
                                        
                                    }
                                    else
                                    {
                                        MessageBox.Show("Invalid Input!");
                                    }
                                }
                                else
                                {
                                    firstPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                    ChartType.X1 = Convert.ToInt32(addDialog.XValue);
                                }
                            }
                            else
                            {
                                firstPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                ChartType.X1 = Convert.ToInt32(addDialog.XValue);

                                ChartType.adjustChart();
                                Invalidate();

                                this.firstPointLabel.Location = ChartType.getFirstLabelLocation();
                                this.secondPointLabel.Location = ChartType.getSecondLabelLocation();
                            }
                            
                        }
                        else
                        {
                            MessageBox.Show("Invalid Input!");
                        }
                    }
                    else
                    {
                        if (StringUtil.isNumberic(addDialog.XValue) && Convert.ToDouble(addDialog.XValue)>0)
                        {
                            if (ChartType is TypeSeven)
                            {
                                string txt3 = thirdPointLabel.Text;
                                txt3 = txt3.Substring(1, txt3.Length - 2);
                                string x3SecondLabelValue = txt3.Substring(0, txt3.IndexOf(","));
                                if (StringUtil.isNumberic(x3SecondLabelValue))
                                {
                                    if (Convert.ToInt32(x3SecondLabelValue) > Convert.ToDouble(addDialog.XValue))
                                    {
                                        firstPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                        ChartType.X1 = Convert.ToInt32(addDialog.XValue);
                                    }
                                    else
                                    {
                                        MessageBox.Show("Invalid Input!");
                                    }
                                }
                                else
                                {
                                    firstPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                    ChartType.X1 = Convert.ToInt32(addDialog.XValue);
                                }
                            }
                            else
                            {
                                firstPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                ChartType.X1 = Convert.ToInt32(addDialog.XValue);

                                this.firstPointLabel.Location = ChartType.getFirstLabelLocation();
                                this.secondPointLabel.Location = ChartType.getSecondLabelLocation();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Invalid Input!");
                        }
                    }
                }
            }
        }

        private void secondPointLabel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                AxisLabelAddDialog addDialog = new AxisLabelAddDialog();
                string text = secondPointLabel.Text;
                text = text.Substring(1, text.Length - 2);
                string xValue = text.Substring(0, text.IndexOf(","));
                string yValue = text.Substring(text.IndexOf(",") + 1);

                if (!xValue.Equals("_"))
                {
                    addDialog.XValue = xValue;
                }

                if (!yValue.Equals("_"))
                {
                    addDialog.YValue = yValue;
                }

                addDialog.setYTextBoxUnenable();
                if (addDialog.ShowDialog() == DialogResult.OK)
                {
                    string txt = firstPointLabel.Text;
                    txt = txt.Substring(1, txt.Length - 2);
                    string xFirstLabelValue = txt.Substring(0, txt.IndexOf(","));
                    //firstPointLabel的X轴有数值，则需要与secondLablValue的X轴比较，保证后者大于前者
                    if (StringUtil.isNumberic(xFirstLabelValue))
                    {
                        double firstLabelXNum = Convert.ToDouble(xFirstLabelValue);

                        if (StringUtil.isNumberic(addDialog.XValue) && firstLabelXNum < Convert.ToDouble(addDialog.XValue))
                        {
                            if (ChartType is TypeSeven)
                            {
                                string txt3 = thirdPointLabel.Text;
                                txt3 = txt3.Substring(1, txt3.Length - 2);
                                string x3SecondLabelValue = txt3.Substring(0, txt3.IndexOf(","));

                                if (StringUtil.isNumberic(x3SecondLabelValue))
                                {
                                    if (Convert.ToInt32(x3SecondLabelValue) > Convert.ToDouble(addDialog.XValue))
                                    {
                                        secondPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                        ChartType.X2 = Convert.ToInt32(addDialog.XValue);

                                        ChartType.adjustChart();
                                        Invalidate();

                                        this.firstPointLabel.Location = ChartType.getFirstLabelLocation();
                                        this.secondPointLabel.Location = ChartType.getSecondLabelLocation();
                                        this.thirdPointLabel.Location = ChartType.getThirdLabelLocation();
                                    }
                                    else
                                    {
                                        MessageBox.Show("Invalid Input!");
                                    }
                                }
                                else
                                {
                                    secondPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                    ChartType.X2 = Convert.ToInt32(addDialog.XValue);
                                }
                            }
                            else
                            {
                                secondPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                ChartType.X2 = Convert.ToInt32(addDialog.XValue);

                                ChartType.adjustChart();
                                Invalidate();

                                this.firstPointLabel.Location = ChartType.getFirstLabelLocation();
                                this.secondPointLabel.Location = ChartType.getSecondLabelLocation();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Invalid Input!");
                        }
                    }
                    else 
                    {
                        if (StringUtil.isNumberic(addDialog.XValue) && StringUtil.isNumberic(addDialog.YValue) && Convert.ToDouble(addDialog.XValue) > 0)
                        {
                            if (ChartType is TypeSeven)
                            {
                                string txt3 = thirdPointLabel.Text;
                                txt3 = txt3.Substring(1, txt3.Length - 2);
                                string x3SecondLabelValue = txt3.Substring(0, txt3.IndexOf(","));
                                if (StringUtil.isNumberic(x3SecondLabelValue))
                                {
                                    if (Convert.ToInt32(x3SecondLabelValue) > Convert.ToDouble(addDialog.XValue))
                                    {
                                        secondPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                        ChartType.X2 = Convert.ToInt32(addDialog.XValue);
                                    }
                                    else
                                    {
                                        MessageBox.Show("Invalid Input!");
                                    }
                                }
                                else
                                {
                                    secondPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                    ChartType.X2 = Convert.ToInt32(addDialog.XValue);
                                }
                            }
                            else
                            {
                                secondPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                                ChartType.X2 = Convert.ToInt32(addDialog.XValue);

                                this.firstPointLabel.Location = ChartType.getFirstLabelLocation();
                                this.secondPointLabel.Location = ChartType.getSecondLabelLocation();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Invalid Input!");
                        }
                    }
                }
            }
        }

        private void thirdPointLabel_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Clicks == 2 && e.Button == MouseButtons.Left)
            {
                AxisLabelAddDialog addDialog = new AxisLabelAddDialog();

                string text = thirdPointLabel.Text;
                text = text.Substring(1, text.Length - 2);
                string xValue = text.Substring(0, text.IndexOf(","));
                string yValue = text.Substring(text.IndexOf(",") + 1);

                if (!xValue.Equals("_"))
                {
                    addDialog.XValue = xValue;
                }

                if (!yValue.Equals("_"))
                {
                    addDialog.YValue = yValue;
                }

                if (addDialog.ShowDialog() == DialogResult.OK)
                {
                    string txt1 = firstPointLabel.Text;
                    txt1 = txt1.Substring(1, txt1.Length - 2);
                    string x1FirstLabelValue = txt1.Substring(0, txt1.IndexOf(","));

                    string txt2 = secondPointLabel.Text;
                    txt2 = txt2.Substring(1, txt2.Length - 2);
                    string x2FirstLabelValue = txt2.Substring(0, txt2.IndexOf(","));

                    if (StringUtil.isNumberic(x1FirstLabelValue) && StringUtil.isNumberic(x2FirstLabelValue))
                    {
                        double firstLabelXNum = Convert.ToDouble(x1FirstLabelValue);
                        double secondLabelXNum = Convert.ToDouble(x2FirstLabelValue);

                        if (StringUtil.isNumberic(addDialog.XValue) && StringUtil.isNumberic(addDialog.YValue) && Convert.ToDouble(addDialog.XValue) > secondLabelXNum)
                        {
                            thirdPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";

                            //ChartType.X1 = firstLabelXNum;
                            //ChartType.X2 = secondLabelXNum;
                            ChartType.X3 = Convert.ToInt32(addDialog.XValue);

                            ChartType.adjustChart();
                            Invalidate();

                            this.firstPointLabel.Location = ChartType.getFirstLabelLocation();
                            this.secondPointLabel.Location = ChartType.getSecondLabelLocation();
                            this.thirdPointLabel.Location = ChartType.getThirdLabelLocation();
                        }
                        else
                        {
                            MessageBox.Show("Invalid Input!");
                        }
                    }
                    else if (StringUtil.isNumberic(x1FirstLabelValue))
                    {
                        double firstLabelXNum = Convert.ToDouble(x1FirstLabelValue);

                        if (StringUtil.isNumberic(addDialog.XValue) && Convert.ToDouble(addDialog.XValue) > firstLabelXNum)
                        {
                            thirdPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                            ChartType.X3 = Convert.ToInt32(addDialog.XValue);
                        }
                        else
                        {
                            MessageBox.Show("Invalid Input!");
                        }
                    }
                    else if (StringUtil.isNumberic(x2FirstLabelValue))
                    {
                        double secondLabelXNum = Convert.ToDouble(x2FirstLabelValue);

                        if (StringUtil.isNumberic(addDialog.XValue) && Convert.ToDouble(addDialog.XValue) > secondLabelXNum)
                        {
                            thirdPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                            ChartType.X3 = Convert.ToInt32(addDialog.XValue);
                        }
                        else
                        {
                            MessageBox.Show("Invalid Input!");
                        }
                    }
                    else
                    {
                        if (StringUtil.isNumberic(addDialog.XValue) && Convert.ToDouble(addDialog.XValue) > 0)
                        {
                            thirdPointLabel.Text = "(" + addDialog.XValue + "," + addDialog.YValue + ")";
                            ChartType.X3 = Convert.ToInt32(addDialog.XValue);
                        }
                        else
                        {
                            MessageBox.Show("Invalid Input!");
                        }
                    }               
                }
            }
        }

        #endregion

        
    }
}
